// index.js
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    component: () => import('@/views/layout/FrontLayout.vue'),
    children: [
      {
        path: '',
        name: 'Home',
        component: () => import('@/views/front/Home.vue')
      },
      {
        path: 'test',
        name: 'MbtiTest',
        component: () => import('@/views/front/MbtiTest.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'result',
        name: 'MbtiResult',
        component: () => import('@/views/front/MbtiResult.vue'),
        meta: { requiresAuth: true }
      }
    ]
  },
  {
    path: '/admin/login',
    name: 'Login',
    component: () => import('@/views/admin/Login.vue')
  },
  {
    path: '/admin/register',
    name: 'Register',
    component: () => import('@/views/admin/Register.vue')
  },
  // 学生用户中心
  {
    path: '/student',
    component: () => import('@/views/layout/StudentLayout.vue'),
    meta: { requiresAuth: true, role: 'student' },
    children: [
      {
        path: 'profile',
        name: 'StudentProfile',
        component: () => import('@/views/student/Profile.vue')
      },
      {
        path: 'test-results',
        name: 'StudentTestResults',
        component: () => import('@/views/student/TestResults.vue')
      }
    ]
  },
  // 教师用户中心
  {
    path: '/teacher',
    component: () => import('@/views/layout/TeacherLayout.vue'),
    meta: { requiresAuth: true, role: 'teacher' },
    children: [
      {
        path: 'profile',
        name: 'TeacherProfile',
        component: () => import('@/views/teacher/Profile.vue')
      },
      {
        path: 'students',
        name: 'TeacherStudents',
        component: () => import('@/views/teacher/Students.vue')
      }
    ]
  },
  // 管理员用户中心
  {
    path: '/admin',
    component: () => import('@/views/layout/AdminLayout.vue'),
    meta: { requiresAuth: true, role: 'admin' },
    children: [
      {
        path: 'profile',
        name: 'AdminProfile',
        component: () => import('@/views/admin/Profile.vue')
      },
      {
        path: 'users',
        name: 'UserList',
        component: () => import('@/views/admin/users/UserList.vue')
      },
      {
        path: 'mbti-types',
        name: 'MbtiTypeList',
        component: () => import('@/views/admin/mbti/MbtiTypeList.vue')
      },
      {
        path: 'questions',
        name: 'QuestionList',
        component: () => import('@/views/admin/questions/QuestionList.vue')
      },
      {
        path: 'test-results',
        name: 'TestResultList',
        component: () => import('@/views/admin/test/TestResultList.vue')
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userInfo = localStorage.getItem('user')
  const isAuthenticated = !!userInfo
  const userRole = isAuthenticated ? JSON.parse(userInfo).role : null

  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!isAuthenticated) {
      next({
        path: '/admin/login',
        query: { redirect: to.fullPath }
      })
    } else {
      const requiredRole = to.matched.find(record => record.meta.role)?.meta.role
      if (requiredRole && requiredRole !== userRole) {
        next({
          path: '/',
          query: { redirect: to.fullPath }
        })
      } else {
        next()
      }
    }
  } else {
    if (isAuthenticated && (to.path === '/admin/login' || to.path === '/admin/register')) {
      next('/')
    } else {
      next()
    }
  }
})

export default router